package cat.music.ut.ui.activity

import android.media.MediaPlayer
import android.os.Bundle
import androidx.annotation.Keep
import cat.music.ut.R
import cat.music.ut.base.BaseActivity
import cat.music.ut.comm.viewutils.MyFragmentStateAdapter
import cat.music.ut.component.click
import cat.music.ut.component.log
import cat.music.ut.databinding.ActivityMusicHomeBinding
import cat.music.ut.ui.fragment.DownloadFragment
import cat.music.ut.ui.fragment.IndexFragment
import cat.music.ut.ui.fragment.SearchFragment
import com.angcyo.tablayout.delegate2.ViewPager2Delegate
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class MusicActivity : BaseActivity<ActivityMusicHomeBinding>() {
    private var mMediaPlayer = MediaPlayer()
    override val viewBinding: ActivityMusicHomeBinding
        get() = ActivityMusicHomeBinding.inflate(layoutInflater)
    private val fragments = mutableListOf(IndexFragment(), SearchFragment(), DownloadFragment())

    override fun initView() {
        setTranslucentStatus()
        initHomeNaV()
        startActivity(WelcomeADActivity::class.java)
    }

    override fun iniClick() {
        vb.play.click {
            runCatching {
                if (mMediaPlayer.isPlaying){
                    mMediaPlayer.pause()
                }else{
                    mMediaPlayer.start()
                }
                setPlayState(mMediaPlayer.isPlaying)
            }
        }
    }

    /**
     * 初始化nav布局
     */
    private fun initHomeNaV() {
        ViewPager2Delegate(vb.vp2, vb.dslTab,false)
        vb.vp2.isUserInputEnabled = false
        vb.vp2.adapter = MyFragmentStateAdapter(
            fragments, this
        )
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        EventBus.getDefault().register(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun asdfadf(data:PlayerMusic){
        if (data.play.contains("mflac")){
            toast("暂时无法播放 mflac 格式哦！")
            return
        }
        playMusic(data)
    }
    var play:PlayerMusic? = null
    private fun playMusic(data:PlayerMusic){
        if (play == data) return
        play = data
        setPlayState(true)
        "data.play = ${data.play}".log()
        if (mMediaPlayer.isPlaying) {
            mMediaPlayer.stop()
            mMediaPlayer.release()
        }
        mMediaPlayer = MediaPlayer()
        mMediaPlayer.setDataSource(data.play)
        mMediaPlayer.prepare()
        mMediaPlayer.start()
        vb.tvName.text = data.name
    }
    private fun setPlayState(isPlay:Boolean){
        if(isPlay){
            vb.play.setImageResource(R.drawable.ic_stop)
        }else{
            vb.play.setImageResource(R.drawable.ic_play)
        }
    }
}
@Keep
data class PlayerMusic(val play:String,val name:String)